<?php
require('my_ui.inc');
require('my_cn.inc');
require('my_db.inc');
ui_print_header('projects');
session_start();
//var_dump($_POST);
//var_dump($_GET);

//use REQUEST here for the first project
if(isset($_REQUEST['newproject'])){
	construct_new_project();
}
elseif(isset($_POST['postprject'])){
	post_project();
}
elseif(isset($_POST['showdetails'])){
	show_project_details($_POST['selectpid']);
}
/*
 * navigation
 */
elseif(isset($_POST['nextpage'])
||isset($_POST['prevpage'])
||isset($_POST['firstpage'])
||isset($_POST['showprojects'])){
	construct_projects();
}
else{
	construct_projects();
}
ui_print_footer(date('Y-m-d H:i:s'));
/*
 * provide the default view of project,
 * */
function construct_projects()
{
	$pagerows=2;
	if (isset($_SESSION['currentpage']) && isset($_POST['prevpage']) &&
	$_SESSION['currentpage'] > $pagerows){
		$current = $_SESSION['currentpage'] - $pagerows;
	}
	elseif (isset($_SESSION['currentpage']) && isset($_POST['nextpage'])){
		$current = $_SESSION['currentpage'] + $pagerows;
	}
	elseif (isset($_POST['showpage']) && isset($_SESSION['currentpage'])){
		$current = $_SESSION['currentpage'];
	}
	else{
		$current = 1;
	}
	/*
	 * no photo version this time.
	 * */
	$query='SELECT PID,TITLE, UNAME,DESCRIPTION,POSTTIME
		FROM PROJECT
		ORDER BY POSTTIME';
	$conn=db_connect($err);
	if(!$conn)
	{
		var_dump($query);
		return;
	}
	else
	{
		var_dump($current);
		$prjct=db_get_page_data($conn,$query,$current,$pagerows,$err);
		if($prjct==false)
		{
			//var_dump($err);
			echo '<p>you have reached the last page</p>';
			echo '<p>click<a href="projects.php?newproject">here</a>to post new project</p>';
		}
		//$pid=$prjct[0]['PID'];//unique hander of every project, for further use
		//$_SESSION['pid']=$pid;
		$_SESSION['currentpage']=$current;
		ui_print_projects($prjct,$_SERVER['SCRIPT_NAME']);
		echo '<p>click<a href="projects.php?newproject">here</a>to post new project</p>';
	}
}
/*
 * among a selection on the project, we open a new page for a full,
 * detailed view of the project, including all 3 pics and description
 * */
function show_project_details($pid)
{
	$query='SELECT * FROM PROJECT WHERE PID=:pid';
	$conn=db_connect($err);
	$stmt=oci_parse($conn,$query);
	oci_bind_by_name($stmt,'pid',$pid);
	oci_execute($stmt, OCI_DEFAULT);
	$arr = oci_fetch_assoc($stmt);
	var_dump($arr);
	if($arr){
		$result=$arr['PIC1']->load();
		header("Content-type: image/JPEG");
	echo $result;
	//ui_print_project_details($arr);
	}
	else{
		echo 'no such a project';
	}
}


/*
 * fill the information of a new project,
 * not inserted to database untill post_project is called
 * */
function construct_new_project()
{
	$admin = false;
	if(isset($_SESSION['admin'])&&$_SESSION['admin']==true)
	{
		ui_print_newproject_form($_SERVER['SCRIPT_NAME']);
	}
	else{
		echo '<p>you need to login </p>';
		ui_print_login_form('login.php');
	}
}
function post_project()
{
	//echo 'posted';

	$insstmt='INSERT INTO PROJECT(PID,TITLE,PIC1,PIC2,PIC3,DESCRIPTION,UNAME,POSTTIME)
	VALUES(PID_SEQ.nextval,:title,
	EMPTY_BLOB(),EMPTY_BLOB(),EMPTY_BLOB(),:description,:uname,SYSDATE)
	RETURNING PIC1,PIC2,PIC3 INTO :pic1,:pic2,:pic3';
	$newproject=$_POST;
	$conn=db_connect($err);
	if($conn){
		$stmt=oci_parse($conn,$insstmt);
		$uname=$_SESSION['user'];
		$lob1 = oci_new_descriptor($conn, OCI_D_LOB);
		$lob2 = oci_new_descriptor($conn, OCI_D_LOB);
		$lob3 = oci_new_descriptor($conn, OCI_D_LOB);
		oci_bind_by_name($stmt, ':title', $newproject['title']);
		oci_bind_by_name($stmt, ':description', $newproject['description']);
		oci_bind_by_name($stmt, ':uname', $uname);
		oci_bind_by_name($stmt, ':pic1', $lob1, -1, OCI_B_BLOB);
		oci_bind_by_name($stmt, ':pic2', $lob2, -1, OCI_B_BLOB);
		oci_bind_by_name($stmt, ':pic3', $lob3, -1, OCI_B_BLOB);
		oci_execute($stmt, OCI_DEFAULT);

		if ($lob1->savefile($_FILES['pic1']['tmp_name'])
		&&$lob2->savefile($_FILES['pic2']['tmp_name'])
		&&$lob3->savefile($_FILES['pic3']['tmp_name'])) {
			oci_commit($conn);
			$lob1->free();
			$lob2->free();
			$lob3->free();
		}
		else {
			echo "Couldn't upload\n";
		}
	}
	else
	{
		var_dump($insstmt);
	}
}

?>